<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>购物车布局</title>
  <link href="css/cart.css" rel="stylesheet">
</head>

<body>
  <div id="app" class="container">
    <!-- 空购物车界面 -->
    <div v-if="!hasItems" class="empty-cart-message">
      <div class="empty-cart-image"></div>
      您的购物车空空如也
      <a href="#" class="go-checkout">去购物</a>
    </div>
  
    <div v-else class="cart">
      <h1>科泰淘购物车</h1>
      <div class="clearfix">
        <a href="#" @click.prevent="deleteSelected">删除选中</a>
        <a href="#" @click.prevent="clearCart">清空购物车</a>
      </div>
      <div v-for="(item,index) in cartItems" class="cart-item">
        <div class="cart-item-details">
          <!-- 改变数组中selected的状态 -->
          <input type="checkbox" :checked="item.selected" @change="item.selected = $event.target.checked" class="cart-item-checkbox">
          <img :src="item.image" alt="商品图片">
          <div class="cart-item-name-sku">
            <div class="cart-item-name">{{ item.name }}</div>
            <div class="cart-item-sku">{{ item.sku }}</div>
          </div>
        </div>
        <div class="cart-item-quantity-price">
          <div class="cart-item-quantity">
            <button @click="minus(item)" class="quantity-btn minus-btn">-</button>
            <!-- 无论用户输入的是什么都会将其转换为数据类型 -->
            <input type="text" v-model.number="item.quantity" name="quantity" class="quantity" min="1">
            <button @click="adds(item)" class="quantity-btn plus-btn">+</button>
          </div>
          <!-- 保留两位小数 -->
          <div class="cart-item-price">￥{{ item.price.toFixed(2) }}</div>
        </div>
      </div>
      <div class="total-price-container">
        <div class="total-price">总计：￥{{ totalPrice.toFixed(2) }}</div>
      </div>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <!-- <script src="js/cart-vue.js"></script> -->
  <script>
    new Vue({
      el: '#app',
      data: {
        cartItems: [
          { id: 1, name: '男士休闲裤夏季新款', price: 99.00, quantity: 1, selected: false, sku: '颜色：灰色 尺码：L', image: 'images/goods-1.png' },
          { id: 2, name: '男士夏季装短袖polo衫', price: 199.00, quantity: 1, selected: false, sku: '颜色：浅蓝 尺码：M', image: 'images/goods-2.png' }
        ]
      },
      methods: {
        minus(item) {
          //商品数量减少
          if (item.quantity > 1) {
            item.quantity--;
          }
        },
        adds(item) {
          //商品数量增加
          if(item.quantity < 10){
            item.quantity++;
          }
        },
        deleteSelected() {
          // 过滤掉为选中的商品 保留未选中的商品
          this.cartItems = this.cartItems.filter(item => !item.selected);
        },
        clearCart() {
          //清空购物车所有商品
          this.cartItems = [];
        }
      },
      computed: {
        totalPrice() {
          //返回选中的商品的单价乘以数量的总价格
          return this.cartItems.reduce((total, item) => {
            if (item.selected) {
              return total + item.price * item.quantity;
            }
            return total;
          }, 0);
        },
        hasItems() {
          //当商品小于0时 显示空购物车
          return this.cartItems.length > 0;
        }
      }
    });
  </script>
</body>

</html>